﻿using CaseManagerLibrary.Encrypt;
using CaseManagerLibrary.Http;
using CaseManagerLibrary.Models;
using Newtonsoft.Json;
using PSDLib.Models;
using PSDLib.Protocols;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDManager.SystemSettings.VersionManager
{
    public partial class VersionManagerPanel : UserControl, ISettingPanel
    {
        public ManagerHelper ManagerHelper { get; set; }

        public VersionManagerPanel()
        {
            InitializeComponent();
        }

        public void ApplySettings()
        {
            SaveSettings();
        }

        public void LoadSettings()
        {
            var result_version = HttpHelper.Get<HResult<List<dl_psd_version_vo>>>(AppSettings.Setting.API.GetAllPSDVersion);
            this.lvwVersion.Items.Clear();
            if (result_version.success)
            {
                foreach (var version in result_version.data)
                {
                    this.lvwVersion.Items.Add(new ListViewItem(new string[] {
                        (this.lvwVersion.Items.Count+1).ToString(),
                        version.psd_version,
                        version.description,
                        version.create_time.ToString("yyyy-MM-dd HH:mm:ss"),
                        $@"更新成功 {version.update_success},失败 {version.update_fail},未更新 {version.devcie_count -version.update_success-version.update_fail}"
                    })).Tag = version;
                }

                if (lvwVersion.Items.Count > 0)
                {
                    lvwVersion.Items[0].Selected = true;
                }
            }
        }

        public void SaveSettings()
        {

        }

        private void btnAddVersion_Click(object sender, EventArgs e)
        {
            FrmVersion frm = new FrmVersion();
            frm.StartPosition = FormStartPosition.CenterScreen;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                var result = HttpHelper.Post<HResult>(AppSettings.Setting.API.AddPSDVersion, JsonConvert.SerializeObject(frm.Version));

                if (result.success)
                {
                    result = UploadVersionFile(frm.VersionFiles);

                    if (result.success)
                    {
                        result = HttpHelper.Post<HResult>(AppSettings.Setting.API.AddPSDVersionFiles, JsonConvert.SerializeObject(frm.VersionFiles));

                        if (result.success)
                        {
                            lvwVersion.Items.Add(new ListViewItem(new string[] {
                            (this.lvwVersion.Items.Count+1).ToString(),
                            frm.Version.psd_version,
                            frm.Version.description,
                            frm.Version.create_time.ToString("yyyy-MM-dd HH:mm:ss"),
                            $@"更新成功 {frm.Version.update_success},失败 {frm.Version.update_fail}"
                        })).Tag = frm.Version;
                        }
                    }
                }
            }
        }

        private HResult UploadVersionFile(List<dl_psd_version_file_vo> files)
        {
            HResult result = new HResult() { success = false };

            foreach (var file in files)
            {
                result = ManagerHelper.UploadVersionFile(file);

                if (!result.success)
                {
                    break;
                }
            }

            return result;
        }



        private void btnEditVersion_Click(object sender, EventArgs e)
        {

        }

        private void btnDelVersion_Click(object sender, EventArgs e)
        {

        }

        private void btnUpdateVersion_Click(object sender, EventArgs e)
        {

        }
    }
}
